using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using Telerik.WinControls.UI;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class ListBulkProduct : Telerik.WinControls.UI.RadForm
    {
        B_BulkProduct B_bulkproduct = new B_BulkProduct();
        B_Farmer B_farmer = new B_Farmer();

        int bulkProductID, farmID;
        string productName;
        public ListBulkProduct()
        {
            InitializeComponent();
        }

        //load context menu
        private void loadContextMenu()
        {
            ContextMenu menu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "Delete";
            menu.MenuItems.Add(item);
            grvBulkProduct.ContextMenu = menu;
            item.Click += new EventHandler(item_Click);
        }

        void item_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this,"Are you sure","Deleting product",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                int bulkProductID = int.Parse(grvBulkProduct.SelectedRows[0].Cells["BulkProductID"].Value.ToString());
                bool isOk = B_bulkproduct.RemoveOneBulkProduct(bulkProductID);
                if (isOk)
                {
                    MessageBox.Show(this, "Product deleted successful", "Product deleting", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadBulkProduct();
                }
                else
                {
                    MessageBox.Show(this, "Can not delete this product", "Product deleting", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void ListBulkProduct_Load(object sender, EventArgs e)
        {
            loadBulkProduct();
            loadContextMenu();
        }

        //load bulk product
        private void loadBulkProduct()
        {
            DataTable aTable = B_bulkproduct.GetAllBulkProduct();
            grvBulkProduct.DataSource = aTable;
        }

        private void grvBulkProduct_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvBulkProduct.SelectedRows.Count > 0)
                {
                    GridViewRowInfo row = grvBulkProduct.SelectedRows[0];
                    bulkProductID = int.Parse(row.Cells["BulkProductID"].Value.ToString());
                    farmID = int.Parse(row.Cells["FarmID"].Value.ToString());
                    productName = row.Cells["BulkProductName"].Value.ToString();

                    Farmer_Info farmerInfo = B_farmer.GetOneFarmer(farmID);
                    txtOwnerName.Text = farmerInfo.FarmerName;
                    txtProductName.Text = productName;
                }
            }
            catch (Exception ex) { }
        }

        private void btnSaveChange_Click(object sender, EventArgs e)
        {
            try
            {
                BulkProduct_Info info = new BulkProduct_Info();
                info.BulkProductID = bulkProductID;
                info.BulkProductName = txtProductName.Text;
                info.FarmID = farmID;
                bool isOk = B_bulkproduct.EditOneBulkProduct(info);
                if (isOk)
                {
                    MessageBox.Show(this, "Product updated successful", "Product updating", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadBulkProduct();
                }
                else
                {
                    MessageBox.Show(this, "Product updating is fail", "Product updating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) { }
        }
    }
}
